﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Model;
using System.Data;

namespace Forest.Lion.Data.SqlServer.Repositories {
    public class ContractRepository : BaseRepository, IContractRepository {
        public ContractRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Contract contract) {
            this.GetDbSet<Contract>().Add(contract);
            this.UnitOfWork.SaveChanges();
        }

        public Contract GetContract(Guid contractId) {
            return this.GetDbSet<Contract>()
                .Where(c => c.Id == contractId)
                .Single();
        }

        public void Update(Contract updatedContract) {
            this.GetDbSet<Contract>().Attach(updatedContract);
            this.SetEntityState(updatedContract, updatedContract.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid contractId) {
            Contract contractToDelete = this.GetContract(contractId);
            this.GetDbSet<Contract>().Remove(contractToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Contract> Find() {
            return this.GetDbSet<Contract>().Take(100)
                .ToList();
        }

        public IEnumerable<Contract> GetAll() {
            return this.GetDbSet<Contract>().Take(100)
                .ToList();
        }

        public Contract GetByCompanyId(Guid companyId) {
            return this.GetDbSet<Contract>()
                .Where(c => c.CompanyId == companyId)
                .Single();
        }

        public IEnumerable<Contract> GetContracts(Guid companyId) {
            return this.GetDbSet<Contract>().Where(c => c.CompanyId == companyId).ToList();

        }
    }

}
